﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Kinect;

namespace Kinect.Toolbox.Xna
{
    public class CursorXna : MouseController
    {

        protected GraphicsDeviceManager MyGraphics;
        public CursorXna(GraphicsDeviceManager graphics) : base(graphics.PreferredBackBufferWidth,graphics.PreferredBackBufferHeight)
        {
            this.MyGraphics = graphics;
        }



        public override Vector2Struct UpdateCursorPosition(Joint joint)
        {
            Vector2Struct vector2 = FilterJointPosition(sensor, joint);

            bool isClicked = false;

            if (ClickGestureDetector == null)
            {
                isClicked = Math.Abs(joint.Position.Z - previousDepth) > 0.05f;
                justClicked = isClicked;
                if (justClicked && MouseClicked != null)
                {
                    MouseClicked(this, new ClickEventArgs(true));
                    justClickedDepth = joint.Position.Z;
                }
            }
            else
                isClicked = clickGestureDetected;

            if (joint.Position.Z - justClickedDepth > 0.05 && MouseClicked != null)
            {
                MouseClicked(this, new ClickEventArgs(false));
            }



            vector2.X = vector2.X * 1.4f - 0.2f;
            vector2.Y = vector2.Y * 1.4f - 0.2f;
            if (vector2.X < 0.0f)
                vector2.X = 0.0f;
            else if (vector2.X > 1.0f)
                vector2.X = 1.0f;
            if (vector2.Y < 0.0f)
                vector2.Y = 0.0f;
            else if (vector2.Y > 1.0f)
                vector2.Y = 1.0f;
            Vector2Struct screenPosition = new Vector2Struct(
                (int)((vector2.X) * ScreenWidth),
                (int)((vector2.Y) * ScreenHeight));
            previousDepth = joint.Position.Z;

            clickGestureDetected = false;
            return screenPosition;
        }
    }
}
